package com.ch.dream.logic.main;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.ch.dream.R;
import com.ch.dream.activity.longstory.MyDreamActivity;
import com.ch.dream.activity.longstory.MyStoryActivity;
import com.ch.dream.activity.longstory.StoryRecommendActivity;
import com.ch.dream.activity.main.MainMenuActivity;
import com.ch.dream.activity.shortstory.CollectActivity;
import com.ch.dream.activity.shortstory.EventInputActivity;
import com.ch.dream.activity.shortstory.PeopleInputActivity;
import com.ch.dream.activity.shortstory.PlaceInputActivity;
import com.ch.dream.activity.shortstory.StoryContentActivity;
import com.ch.dream.activity.shortstory.TimeInputActivity;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.common.CommonUserInfo;
import com.ch.dream.database.MyDatabase;
import com.ch.dream.tools.ViewTools;

public class MainMenuLogic extends BaseLogic {

	public MainMenuLogic(MainMenuActivity activity) {
		super(activity);
	}

	public MainMenuLogic(MainMenuActivity activity, int layoutId) {
		super(activity, layoutId);
	}

	private GridView sGridView;//短故事menu
	private GridAdapter sAdapter;//短故事adapter
	private GridView lGridView;//长故事menu
	private GridAdapter lAdapter;//长故事adapter
	private ArrayList< HashMap<String, Object>> sList;//短故事list
	private ArrayList< HashMap<String, Object>> lList;//长故事list
	private String[] sItemText = {"时间","地点","人物","事件","看故事","收藏"};//短故事内容
	private String[] lItemText = {"我的故事","我的梦境","今日推荐"};//长故事内容
//	private List<Integer> sItemNum;
	private int timeNum;
	private int placeNum;
	private int eventNum;
	private int peopleNum;
	private int getNum;
	public void init() {
		initSGridView();
		initLGridView();
	}
	/**
	 * 初始化时间地点人物事件回复数与获取总数
	 */
	private void initReplyNum(){
//		SharedPreferences  spReply =activity.getSharedPreferences(CommonData.REPLYNUM,Context.MODE_PRIVATE);
//		timeNum =spReply.getInt("timeNum", CommonData.DEFAULTREPLYNUM);
//		placeNum = spReply.getInt("placeNum", CommonData.DEFAULTREPLYNUM);
//		eventNum = spReply.getInt("eventNum", CommonData.DEFAULTREPLYNUM);
//		peopleNum = spReply.getInt("peopleNum", CommonData.DEFAULTREPLYNUM);
//		getNum = spReply.getInt("getNum", CommonData.DEFAULTREPLYNUM);
//		sItemNum =new ArrayList<Integer>();
//		sItemNum.add(timeNum);
//		sItemNum.add(placeNum);
//		sItemNum.add(peopleNum);
//		sItemNum.add(eventNum);
//		sItemNum.add(getNum);
	}
	
	
	/**
	 * 初始化短故事列表
	 */
	private void initSGridView(){
//		initReplyNum();
		sGridView = (GridView) activity.findViewById(R.id.sGridview);
		sList = new ArrayList<HashMap<String,Object>>();
		HashMap<String,Object> sMap = new HashMap<String, Object>();
		for(int i=0;i<sItemText.length;i++){
			sMap = new HashMap<String, Object>();
//			if(sItemNum.size()>i){
//				sMap.put("itemText", sItemText[i]+sItemNum.get(i));
//				sList.add(sMap);
//			}else{
				sMap.put("itemText", sItemText[i]);
				sList.add(sMap);
//			}
		
		}
		sAdapter = new GridAdapter(activity, sList);
		sGridView.setAdapter(sAdapter);
		sGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
				Intent intent =activity.getIntent();
				switch (position) {
				case 0:
//					if(timeNum>0){
						intent.setClass(activity, TimeInputActivity.class);
						activity.startActivity(intent);
//					}else{
//						ViewTools.showShortToast(activity, "您今天创造的"+sItemText[position].replaceAll("[0-9]", "")+"太多了，试试其他的吧");
//					}
					break;

				case 1:
//					if(placeNum>0){
						intent.setClass(activity, PlaceInputActivity.class);
						activity.startActivity(intent);
//					}else{
//						ViewTools.showShortToast(activity, "您今天创造的"+sItemText[position].replaceAll("[0-9]", "")+"太多了，试试其他的吧");
//					}
					break;
				case 2:
//					if(peopleNum>0){
						intent.setClass(activity, PeopleInputActivity.class);
						activity.startActivity(intent);
//					}else{
//						ViewTools.showShortToast(activity, "您今天创造的"+sItemText[position].replaceAll("[0-9]", "")+"太多了，试试其他的吧");
//					}
					break;
				case 3:
//					if(eventNum>0){
						intent.setClass(activity, EventInputActivity.class);
						activity.startActivity(intent);
//					}else{
//						ViewTools.showShortToast(activity, "您今天创造的"+sItemText[position].replaceAll("[0-9]", "")+"太多了，试试其他的吧");
//					}
					break;
				case 4:
//					if(getNum>0){
						if(CommonUserInfo.userId==null || "".equals(CommonUserInfo.userId)){
							ViewTools.showShortToast(activity, "只有会员才可以看故事");
							return;
						}
						intent.setClass(activity, StoryContentActivity.class);
						intent.putExtra("getStory", true);
						activity.startActivity(intent);
//					}else{
//						ViewTools.showShortToast(activity, "您今天白看的太多了，试试自己创造一个吧");
//					}
					break;
				case 5:
					MyDatabase myDatabase = new MyDatabase(activity);
					Cursor c = null;
					try{
						c = myDatabase.query(MyDatabase.shortStoryCollect, new String[]{"_id","storyId","content","createTime"}
						, null, null, null);
						if(c.getCount()>0){
							intent.setClass(activity, CollectActivity.class);
							activity.startActivity(intent);
							break;
						}
						ViewTools.showShortToast(activity, "您还没有收藏任何故事");
						break;
					}catch (Exception e) {
						e.printStackTrace();
					}finally{
						if(c!=null){
							c.close();
						}
						myDatabase.close();
					}
					
					break;
				default:
					break;
				}
			}
		});
	}

	/**
	 * 初始化长故事列表
	 */
	private void initLGridView(){
		lGridView = (GridView) activity.findViewById(R.id.lGridview);
		lList = new ArrayList<HashMap<String,Object>>();
		HashMap<String,Object> lMap = new HashMap<String, Object>();
		for(int i=0;i<lItemText.length;i++){
			lMap = new HashMap<String, Object>();
			lMap.put("itemText", lItemText[i]);
			lList.add(lMap);
		}
		lAdapter = new GridAdapter(activity, lList);
		lGridView.setAdapter(lAdapter);
		lGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long id) {
				Intent intent =activity.getIntent();
				switch (position) {
				case 0://我的故事
					intent.setClass(activity, MyStoryActivity.class);
					if(CommonUserInfo.userId==null || "".equals(CommonUserInfo.userId)){
						ViewTools.showShortToast(activity, "请先登录");
						return;
					}
					break;
				case 1://我的梦境
					intent.setClass(activity, MyDreamActivity.class);
					if(CommonUserInfo.userId==null || "".equals(CommonUserInfo.userId)){
						ViewTools.showShortToast(activity, "请先登录");
						return;
					}
					break;
				case 2://今日推荐
					intent.setClass(activity, StoryRecommendActivity.class);
					break;
				default:
					break;
				}
				activity.startActivity(intent);
			}
		});
	}
	
	public class GridAdapter extends BaseAdapter {

		private class GridHolder {
			TextView itemTextView;
		}

		private Context context;

		private ArrayList<HashMap<String, Object>> list;
		private LayoutInflater mInflater;

		public GridAdapter(Context c, ArrayList<HashMap<String, Object>> list) {
			super();
			this.context = c;
			this.list = list;
			mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int index) {
			return list.get(index);
		}

		@Override
		public long getItemId(int index) {
			return index;
		}

		@Override
		public View getView(int index, View convertView, ViewGroup parent) {
			GridHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.m_menu_item, null);
				holder = new GridHolder();
				holder.itemTextView = (TextView) convertView.findViewById(R.id.itemTextView);
				convertView.setTag(holder);
			} else {
				holder = (GridHolder) convertView.getTag();
			}
			HashMap<String, Object> map = list.get(index);
			if (map != null) {
				holder.itemTextView.setText(map.get("itemText").toString());
			}
			return convertView;
		}

	}

}
